package com.bcmusic.user.service.impl;

import com.bcmusic.user.dao.DistributorIdentificationMapper;
import com.bcmusic.user.pojo.DistributorIdentification;
import com.bcmusic.user.service.DistributorIdentificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:DistributorIdentification业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DistributorIdentificationServiceImpl implements DistributorIdentificationService {

    @Autowired
    private DistributorIdentificationMapper distributorIdentificationMapper;


    /**
     * DistributorIdentification条件+分页查询
     * @param distributorIdentification 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<DistributorIdentification> findPage(DistributorIdentification distributorIdentification, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(distributorIdentification);
        //执行搜索
        return new PageInfo<DistributorIdentification>(distributorIdentificationMapper.selectByExample(example));
    }

    /**
     * DistributorIdentification分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<DistributorIdentification> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<DistributorIdentification>(distributorIdentificationMapper.selectAll());
    }

    /**
     * DistributorIdentification条件查询
     * @param distributorIdentification
     * @return
     */
    @Override
    public List<DistributorIdentification> findList(DistributorIdentification distributorIdentification){
        //构建查询条件
        Example example = createExample(distributorIdentification);
        //根据构建的条件查询数据
        return distributorIdentificationMapper.selectByExample(example);
    }


    /**
     * DistributorIdentification构建查询对象
     * @param distributorIdentification
     * @return
     */
    public Example createExample(DistributorIdentification distributorIdentification){
        Example example=new Example(DistributorIdentification.class);
        Example.Criteria criteria = example.createCriteria();
        if(distributorIdentification!=null){
            // 分发商身份认证记录id。记录在提交身份认证/修改申请的时候创建。
            if(!StringUtils.isEmpty(distributorIdentification.getIdentificationId())){
                    criteria.andEqualTo("identificationId",distributorIdentification.getIdentificationId());
            }
            // 分发商编号。关联distributor表的distributor_number字段。
            if(!StringUtils.isEmpty(distributorIdentification.getDistributorNumber())){
                    criteria.andEqualTo("distributorNumber",distributorIdentification.getDistributorNumber());
            }
            // 分发商名称。
            if(!StringUtils.isEmpty(distributorIdentification.getDistributorName())){
                    criteria.andEqualTo("distributorName",distributorIdentification.getDistributorName());
            }
            // 统一社会信用代码。位数为18位。
            if(!StringUtils.isEmpty(distributorIdentification.getDistributorUscc())){
                    criteria.andEqualTo("distributorUscc",distributorIdentification.getDistributorUscc());
            }
            // 分发商负责人姓名。
            if(!StringUtils.isEmpty(distributorIdentification.getDistributorManager())){
                    criteria.andEqualTo("distributorManager",distributorIdentification.getDistributorManager());
            }
            // 分发商银行卡号。银行卡号长度一般在13-19位，国际上也有12位的。
            if(!StringUtils.isEmpty(distributorIdentification.getBankCardId())){
                    criteria.andEqualTo("bankCardId",distributorIdentification.getBankCardId());
            }
            // 分发商地址。
            if(!StringUtils.isEmpty(distributorIdentification.getDistributorLocation())){
                    criteria.andEqualTo("distributorLocation",distributorIdentification.getDistributorLocation());
            }
            // 分发商邮箱。用来接收通知。
            if(!StringUtils.isEmpty(distributorIdentification.getDistributorMail())){
                    criteria.andEqualTo("distributorMail",distributorIdentification.getDistributorMail());
            }
            // 分发商部署节点数量。分发商申请加入联盟链时，申请维护的节点数量。
            if(!StringUtils.isEmpty(distributorIdentification.getNodeCount())){
                    criteria.andEqualTo("nodeCount",distributorIdentification.getNodeCount());
            }
            // 分发商身份认证状态。0默认为未申请，1为待审核，2为已认证，-1为已驳回，-2为已废除。任意类型管理员修改分发商的信息都默认为已认证状态。

            if(!StringUtils.isEmpty(distributorIdentification.getValidStatus())){
                    criteria.andEqualTo("validStatus",distributorIdentification.getValidStatus());
            }
            // 提交者编号。默认为提交认证/修改申请的分发商或管理员编号，任意类型管理员修改分发商信息时，申请者为该管理员。
            if(!StringUtils.isEmpty(distributorIdentification.getSubmitterNumber())){
                    criteria.andEqualTo("submitterNumber",distributorIdentification.getSubmitterNumber());
            }
            // 审核者编号。此处为任意类型的管理员。
            if(!StringUtils.isEmpty(distributorIdentification.getAuditorNumber())){
                    criteria.andEqualTo("auditorNumber",distributorIdentification.getAuditorNumber());
            }
            // 提交认证/修改申请时间。
            if(!StringUtils.isEmpty(distributorIdentification.getSubmitTime())){
                    criteria.andEqualTo("submitTime",distributorIdentification.getSubmitTime());
            }
            // 审核认证/修改申请时间。
            if(!StringUtils.isEmpty(distributorIdentification.getCheckTime())){
                    criteria.andEqualTo("checkTime",distributorIdentification.getCheckTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        distributorIdentificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改DistributorIdentification
     * @param distributorIdentification
     */
    @Override
    public void update(DistributorIdentification distributorIdentification){
        distributorIdentificationMapper.updateByPrimaryKey(distributorIdentification);
    }

    /**
     * 增加DistributorIdentification
     * @param distributorIdentification
     */
    @Override
    public DistributorIdentification add(DistributorIdentification distributorIdentification){
        distributorIdentificationMapper.insertSelective(distributorIdentification);
        return distributorIdentificationMapper.selectByPrimaryKey(distributorIdentification.getIdentificationId());
    }

    /**
     * 根据ID查询DistributorIdentification
     * @param id
     * @return
     */
    @Override
    public DistributorIdentification findById(Long id){
        return  distributorIdentificationMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询DistributorIdentification全部数据
     * @return
     */
    @Override
    public List<DistributorIdentification> findAll() {
        return distributorIdentificationMapper.selectAll();
    }

    @Override
    public DistributorIdentification updateSelective(DistributorIdentification distributorIdentification) {
        distributorIdentificationMapper.updateByPrimaryKeySelective(distributorIdentification);
        return distributorIdentificationMapper.selectByPrimaryKey(distributorIdentification.getIdentificationId());
    }
}
